//对 axios 进行二次封装
import axios from "axios"

//引入进度条
import Nprogress from "nprogress"
import 'nprogress/nprogress.css'

import store from '@/store'

const requests = axios.create({
    //配置基础路径
    baseURL:'/api',
    //配置请求超时时间
    timeout:5000
})
//请求拦截器
requests.interceptors.request.use((config)=>{
    
    if (store.state.detail.uuid_token) {
        config.headers.userTempId = store.state.detail.uuid_token
    }

    if(store.state.user.token) {
        config.headers.token = store.state.user.token
    }

    Nprogress.start()
    return config
})
//响应拦截器
requests.interceptors.response.use((res)=>{
    //成功的回调函数
    Nprogress.done()
    return res.data
}, (error)=>{
    //响应失败的回调
    return Promise.reject(new Error('faile'))
})

//
export default requests
